﻿using System;
using System.Linq;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.CanBoQuanLyForm;
using ComputerVision.CapPheDuyetForm;
using ComputerVision.CapQuanLyHanhChinhForm;
using ComputerVision.CoQuanQuanLyPCCCForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.DoiForm;
using ComputerVision.HinhThucDauTuForm;
using ComputerVision.LoaiCoSoForm;
using ComputerVision.LoaiHinhHoatDongForm;
using ComputerVision.NganhForm;
using ComputerVision.PhongForm;
using ComputerVision.Services;
using ComputerVision.ThanhPhanKinhTeForm;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmPhieuDieuTraCoSoDetail : frmObject
    {
        private readonly Guid _idPhieu;
        private readonly IService<Phong> _phong;
        private readonly IService<Doi> _doi;
        private readonly IService<CapQuanLyHanhChinh> _capQuanLyHanhChinh;
        private readonly IService<CanBo> _canBo;
        private IService<LoaiCoSo> _loaiCoSo;
        private readonly IService<CoQuanQuanLyPCCC> _coQuanQuanLyPccc;
        private readonly IService<HinhThucDauTu> _hinhThucDauTu;
        private readonly IService<QuanHuyen> _quanHuyen;
        private readonly IService<LoaiHinhHoatDong> _loaiHinhHoatDong;
        private readonly IService<ThanhPhanKinhTe> _thanhPhanKinhTe;
        private readonly IService<Nganh> _nganh;
        private readonly IService<CapPheDuyet> _capPheDuyet;
        private IService<ThamDuyetPCCC> _thamDuyet;
        private IService<HangMuc> _hangMuc;
        private IService<PhuongTienCoSo> _phuongTienCoSo;
        private IService<NguonNuoc> _nguonNuoc;
        private IService<TuyenTruyenHuanLuyen> _tuyenTruyenHuanLuyen;
        private IService<KiemTraAnToan> _kiemTraAnToan;
        private IService<PhuongAnChuaChay> _phuongAnChuaChay;
        private IService<XuLyViPham> _xuLyViPham;
        private IService<TinhHinhChayNo> _tinhHinhChayNo;
        private IService<ThayDoiCoSo> _thayDoiCoSo;
        private readonly IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;


        #region "Delegate"

        public delegate void LoadData();

        public event LoadData OnLoadData = null;

        protected virtual void FireEvent()
        {
            if (OnLoadData != null)
            {
                OnLoadData();
            }
        }

        #endregion

        public frmPhieuDieuTraCoSoDetail()
        {
            InitializeComponent();
            _idPhieu = Guid.NewGuid();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doi = Program.Container.Resolve<IService<Doi>>();
            _capQuanLyHanhChinh = Program.Container.Resolve<IService<CapQuanLyHanhChinh>>();
            _coQuanQuanLyPccc = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _canBo = Program.Container.Resolve<IService<CanBo>>();
            _quanHuyen = Program.Container.Resolve<IService<QuanHuyen>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _hinhThucDauTu = Program.Container.Resolve<IService<HinhThucDauTu>>();
            _loaiHinhHoatDong = Program.Container.Resolve<IService<LoaiHinhHoatDong>>();
            _thanhPhanKinhTe = Program.Container.Resolve<IService<ThanhPhanKinhTe>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            _tuyenTruyenHuanLuyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            _kiemTraAnToan = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _phuongAnChuaChay = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            _capPheDuyet = Program.Container.Resolve<IService<CapPheDuyet>>();
            _tinhHinhChayNo = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            _thayDoiCoSo = Program.Container.Resolve<IService<ThayDoiCoSo>>();
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();

            txtSo.Text = Constants.SoDetail.TenSo;
            LoadPhong();
            LoadDoi();
            LoadQuanHuyen();
            LoadCoQuanQl();
            LoadCanBo();
            LoadLoaiCoSo();
            LoadHinhThucDauTu();
            LoadLoaiHinhHoatDong();
            LoadThanhPhanKinhTe();
            LoadNganh();
            LoadCapPheDuyet();
            LoadCapQuanLyHanhChinh();

            //Insert temp record
            InsertTempRecord();
        }

        public frmPhieuDieuTraCoSoDetail(Guid id)
        {
            InitializeComponent();
            _idPhieu = id;
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doi = Program.Container.Resolve<IService<Doi>>();
            _capQuanLyHanhChinh = Program.Container.Resolve<IService<CapQuanLyHanhChinh>>();
            _coQuanQuanLyPccc = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _canBo = Program.Container.Resolve<IService<CanBo>>();
            _quanHuyen = Program.Container.Resolve<IService<QuanHuyen>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _hinhThucDauTu = Program.Container.Resolve<IService<HinhThucDauTu>>();
            _loaiHinhHoatDong = Program.Container.Resolve<IService<LoaiHinhHoatDong>>();
            _thanhPhanKinhTe = Program.Container.Resolve<IService<ThanhPhanKinhTe>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            _tuyenTruyenHuanLuyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            _kiemTraAnToan = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _phuongAnChuaChay = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            _capPheDuyet = Program.Container.Resolve<IService<CapPheDuyet>>();
            _tinhHinhChayNo = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            _thayDoiCoSo = Program.Container.Resolve<IService<ThayDoiCoSo>>();
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();

            txtSo.Text = Constants.SoDetail.TenSo;
            LoadPhong();
            LoadDoi();
            LoadQuanHuyen();
            LoadCoQuanQl();
            LoadCanBo();
            LoadLoaiCoSo();
            LoadHinhThucDauTu();
            LoadLoaiHinhHoatDong();
            LoadThanhPhanKinhTe();
            LoadNganh();
            LoadCapPheDuyet();
            LoadCapQuanLyHanhChinh();

            var item = _phieuDieuTraCoSo.Detail(_idPhieu);
            if (item != null)
            {
                dtNgayLapPhieu.Value = item.NgayLapPhieu.Value;
                txtSoPhieu.Text =item.SoPhieu;
                drlCanBoQuanLy.SelectedValue = item.CanBoQuanLyId;
                drlCoQuanQL.SelectedValue=item.CoQuanQuanLyPCCCId;
                drlLoaiCoSo.SelectedValue=item.LoaiCoSoId;
                drlPhong.SelectedValue = item.CanBo.Doi.Phong_Id;
                drlDoi.SelectedValue = item.CanBo.Doi_Id;
                txtTenCoSo.Text = item.TenCoSo;
                txtEmail.Text =item.Email;
                txtWebsite.Text =item.Website;
                textNamThanhLap.Text = item.NamThanhLap;
                txtDienThoai.SelectedText = item.DienThoai;
                txtDiaChi_So.Text=item.DiaChi_So;
                txtDiaChi_Duong.Text=item.DiaChi_Duong;
                txtDiaChi_Phuong.Text =item.DiaChi_Phuong;
                drlQuanHuyen.SelectedValue = item.QuanHuyenId;
                txtDuongDi.Text =item.DuongDi;
                txtKhu.Text=item.Khu;
                txtHoTenNguoiDungDauCS.Text=item.NguoiDungDauCoSo;
                txtDiDong.Text =item.DiDong;
                txtCoQuanQLTrucTiep.Text =item.CoQuanQuanLyTrucTiep;
                txtNuocDauTu.Text =item.NuocDauTu;
                drlCapQuanLyHanhChinh.SelectedValue=item.CapQuanLyHanhChinhId;
                drlHinhThucDauTu.SelectedValue=item.HinhThucDauTuId;
                drlThanhPhanKinhTe.SelectedValue=item.ThanhPhanKinhTeId;
                drlLoaiHinhHoatDong.SelectedValue=item.LoaiHinhHoatDongId;
                drlNganh.SelectedValue = item.NganhId;

                txtTinhChatHoatDong.Text = item.TinhChatHoatDong;
                if (item.CoSoThuocDienMuaBHCNBB == true)
                    radDamua.Checked = true;
                else
                    radChuaMua.Checked = true;

                if (item.CoSoKhongThuocDienMuaBHCNBB == true)
                    radKhongMua.Checked = true;
                else
                    radCoMua.Checked = true;

                txtLucLuong_TongSo.Text = item.LLPCCC_TongSoNguoi.ToString();
                txtLucLuong_SoNguoiDuocCapGiay.Text = item.LLPCCC_SoNguoiDuocCapGiayPhep.ToString();
                txtLucLuong_TrongGioLamViec.Text = item.LLPCCC_TrongGioLamViec.ToString();
                txtLucLuong_NgoaiGioLamViec.Text = item.LLPCCC_NgoaiGioLamViec.ToString();
                txtLucLuong_BoPhanCongTac.Text = item.LLPCCC_BoPhanCongTac.ToString();
                txtLucLuong_DiDong.Text = item.LLPCCC_DienThoaiDiDong.ToString();
                txtLucLuong_DienThoai.Text = item.LLPCCC_DienThoaiCoDinh.ToString();
                txtLucLuong_TenDoiTruong.Text = Convert.ToString(item.LLPCCC_HoTenDoiTruong);

                txtKiemTra_SoLan.Text = item.KT_SoLanKiemTra.ToString();

                if (item.TT_NgayXayDungPA.HasValue)
                {
                    dtPACC_Ngay.Value = item.TT_NgayXayDungPA.Value;
                }

                //txtPACC_TT.Text = item.TT_PA;
                drlCapPheDuyet.SelectedValue = item.CapPheDuyetId;

                LoadDataThamDuyet();
                LoadDataHangMuc();
                LoadDataPhuongTienPcCc();
                LoadDataNguonNuoc();
                LoadDataTuyenTruyen();
                LoadDataKiemTraAnToan();
                LoadDataXuLyViPham();
                LoadDataPhuongAnCc();
                LoadDataTinhHinhChayNo();
                LoadDataThayDoiCoSo();

                //item.NuocDauTu = txtNuocDauTu.Text.Trim();
                //item.HinhThucDauTuId = Guid.Parse(drlHinhThucDauTu.SelectedValue.ToString());
                //item.ThanhPhanKinhTeId = Guid.Parse(drlThanhPhanKinhTe.SelectedValue.ToString());
                //item.LoaiHinhHoatDongId = Guid.Parse(drlLoaiHinhHoatDong.SelectedValue.ToString());
                //item.NganhId = Guid.Parse(drlNganh.SelectedValue.ToString());
                //item.TinhChatHoatDong = txtTinhChatHoatDong.Text.Trim();
                //if (radDamua.Checked)
                //{
                //    item.CoSoThuocDienMuaBHCNBB = true;
                //}
                //else
                //{
                //    item.CoSoThuocDienMuaBHCNBB = false;
                //}

                //if (radKhongMua.Checked)
                //{
                //    item.CoSoKhongThuocDienMuaBHCNBB = true;
                //}
                //else
                //{
                //    item.CoSoKhongThuocDienMuaBHCNBB = false;
                //}

                //item.LLPCCC_TongSoNguoi = (int?)txtLucLuong_TongSo.Value;
                //item.LLPCCC_SoNguoiDuocCapGiayPhep = (int?)txtLucLuong_SoNguoiDuocCapGiay.Value;
                //item.LLPCCC_TrongGioLamViec = (int?)txtLucLuong_TrongGioLamViec.Value;
                //item.LLPCCC_NgoaiGioLamViec = (int?)txtLucLuong_NgoaiGioLamViec.Value;
                //item.LLPCCC_BoPhanCongTac = txtLucLuong_BoPhanCongTac.Text.Trim();
                //item.LLPCCC_DienThoaiCoDinh = txtLucLuong_DienThoai.Text.Trim();
                //item.LLPCCC_DienThoaiDiDong = txtLucLuong_DiDong.Text.Trim();
                //item.KT_SoLanKiemTra = (int?)txtKiemTra_SoLan.Value;
                //item.TT_NgayXayDungPA = dtPACC_Ngay.Value;
                //item.TT_PA = txtPACC_TT.Text.Trim();
                //item.CapPheDuyetId = Guid.Parse(drlCapPheDuyet.SelectedValue.ToString());
                //_phieuDieuTraCoSo.Update(item);
            }

        }

        #region Button_Click
        private void btnClose_Click(object sender, EventArgs e)
        {
            var frm = new frmPhieuDieuTraCoSoList();
            frm.MdiParent = MdiParent;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void btnPhong_Click(object sender, EventArgs e)
        {
            var frm = new frmPhongDetail(this);
            frm.ShowDialog();
        }

        private void btnAddDoi_Click(object sender, EventArgs e)
        {
            var frm = new frmDoiDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCoQuanQL_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanQuanLyPCCCDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCanBoQuanLy_Click(object sender, EventArgs e)
        {
            var frm = new frmCanBoQuanLyDetail(this);
            frm.ShowDialog();
        }

        private void btnAddLoaiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiCoSoDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCapQuanLyHanhChinh_Click(object sender, EventArgs e)
        {
            var frm = new frmCapQuanLyHanhChinhDetail(this);
            frm.ShowDialog();
        }

        private void btnAddHinhThucDauTu_Click(object sender, EventArgs e)
        {
            var frm = new frmHinhThucDauTuDetail(this);
            frm.ShowDialog();
        }

        private void btnAddLoaiHinhHoatDong_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiHinhHoatDongDetail(this);
            frm.ShowDialog();
        }

        private void btnAddThanhPhanKinhTe_Click(object sender, EventArgs e)
        {
            var frm = new frmThanhPhanKinhTeDetail(this);
            frm.ShowDialog();
        }

        private void btnAddNganh_Click(object sender, EventArgs e)
        {
            var frm = new frmNganhDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCapPheDuyet_Click(object sender, EventArgs e)
        {
            using (var frm = new frmCapPheDuyetDetail())
            {
                frm.OnLoadData += LoadCapPheDuyet;
                frm.ShowDialog();
            }
        }

        #endregion

        #region Insert temp record
        private void InsertTempRecord()
        {
            try
            {
                var model = new PhieuDieuTraCoSo
                {
                    Id = _idPhieu,
                    Status = false
                };
                _phieuDieuTraCoSo.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Load data to combobox

        public void LoadPhong()
        {
            drlPhong.DataSource = _phong.ToList().ToList().Select(p => new { p.Id, p.TenPhong }).OrderBy(p => p.TenPhong).ToList();
            drlPhong.DisplayMember = "TenPhong";
            drlPhong.ValueMember = "Id";
        }

        public void LoadDoi()
        {
            drlDoi.DataSource = _doi.ToList().ToList().Select(p => new { p.Id, p.TenDoi }).OrderBy(d => d.TenDoi).ToList();
            drlDoi.DisplayMember = "TenDoi";
            drlDoi.ValueMember = "Id";
        }

        public void LoadQuanHuyen()
        {
            drlQuanHuyen.DataSource = _quanHuyen.ToList().ToList().Select(p => new { p.Id, p.Ten }).OrderBy(d => d.Ten).ToList();
            drlQuanHuyen.DisplayMember = "Ten";
            drlQuanHuyen.ValueMember = "Id";
        }

        public void LoadCoQuanQl()
        {
            var source = _coQuanQuanLyPccc.ToList().ToList().Select(p => new { p.Id, p.Ten }).OrderBy(c => c.Ten).ToList();

            drlCoQuanQL.DataSource = source;
            drlCoQuanQL.DisplayMember = "Ten";
            drlCoQuanQL.ValueMember = "Id";
        }

        public void LoadCanBo()
        {
            drlCanBoQuanLy.DataSource = _canBo.ToList().ToList().Select(p => new { p.Id, p.HoTen }).OrderBy(c => c.HoTen).ToList();
            drlCanBoQuanLy.DisplayMember = "HoTen";
            drlCanBoQuanLy.ValueMember = "Id";
        }

        public void LoadLoaiCoSo()
        {
            drlLoaiCoSo.DataSource = _loaiCoSo.ToList().ToList().Select(p => new { p.Id, p.Ten }).OrderBy(p => p.Ten).ToList();
            drlLoaiCoSo.DisplayMember = "Ten";
            drlLoaiCoSo.ValueMember = "Id";
        }

        public void LoadCapQuanLyHanhChinh()
        {
            var source = _capQuanLyHanhChinh.ToList().ToList().Select(p => new { p.Id, p.Ten }).OrderBy(c => c.Ten).ToList();

            drlCapQuanLyHanhChinh.DataSource = source;
            drlCapQuanLyHanhChinh.DisplayMember = "Ten";
            drlCapQuanLyHanhChinh.ValueMember = "Id";
        }

        public void LoadHinhThucDauTu()
        {
            drlHinhThucDauTu.DataSource = _hinhThucDauTu.ToList().ToList().Select(p => new { p.Id, TenHinhThuc = p.Ten }).OrderBy(p => p.TenHinhThuc).ToList();
            drlHinhThucDauTu.DisplayMember = "TenHinhThuc";
            drlHinhThucDauTu.ValueMember = "Id";
        }

        public void LoadLoaiHinhHoatDong()
        {
            drlLoaiHinhHoatDong.DataSource = _loaiHinhHoatDong.ToList().ToList().Select(p => new { p.Id, TenLoaiHinh = p.Ten }).OrderBy(l => l.TenLoaiHinh).ToList();
            drlLoaiHinhHoatDong.DisplayMember = "TenLoaiHinh";
            drlLoaiHinhHoatDong.ValueMember = "Id";
        }

        public void LoadThanhPhanKinhTe()
        {
            drlThanhPhanKinhTe.DataSource = _thanhPhanKinhTe.ToList().ToList().Select(p => new { p.Id, TenTP = p.Ten }).OrderBy(t => t.TenTP).ToList();
            drlThanhPhanKinhTe.DisplayMember = "TenTP";
            drlThanhPhanKinhTe.ValueMember = "Id";
        }

        public void LoadNganh()
        {
            drlNganh.DataSource = _nganh.ToList().ToList().Select(p => new { p.Id, TenNganh = p.Ten }).OrderBy(n => n.TenNganh).ToList();
            drlNganh.DisplayMember = "TenNganh";
            drlNganh.ValueMember = "Id";
        }

        public void LoadCapPheDuyet()
        {
            drlCapPheDuyet.DataSource = _capPheDuyet.ToList().ToList().Select(p => new { p.Id, p.Ten }).OrderBy(p => p.Ten).ToList();
            drlCapPheDuyet.DisplayMember = "Ten";
            drlCapPheDuyet.ValueMember = "Id";
        }
        #endregion

        #region ThamDuyetPCCC

        protected void LoadDataThamDuyet()
        {   
            _thamDuyet = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            thamDuyetPCCCBindingSource.DataSource = _thamDuyet.ToList().ToList()
                .Select(p => new { p.Id, p.SoVanBan, VanBanThamDuyetPCCC = p.VanBanThamDuyetPCCC.Ten, p.NgayCap, CoQuanCapVanBanThamDuyet = p.CoQuanCapVanBanThamDuyet.Ten, p.GhiChu, p.PhieuDieuTraCoSoId })
                .Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu))
                .OrderBy(t => t.NgayCap).ToList();
            grvThamDuyet.Columns[0].Visible = false;
        }

        private void btnAddThamDuyet_Click(object sender, EventArgs e)
        {
            using (var frm = new frmThamDuyetDetail())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataThamDuyet;
                frm.ShowDialog();
            }
        }

        private void btnEditThamDuyet_Click(object sender, EventArgs e)
        {
            if (grvThamDuyet.SelectedRows.Count > 0)
            {
                var selectedRow = grvThamDuyet.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmThamDuyetDetail(id))
                {
                    fEdit.OnLoadData += LoadDataThamDuyet;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelThamDuyet_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvThamDuyet.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvThamDuyet.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _thamDuyet.Detail(id);
                            _thamDuyet.Delete(item);
                            LoadDataThamDuyet();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshThamDuyet_Click(object sender, EventArgs e)
        {
            LoadDataThamDuyet();
        }

        #endregion

        #region HangMuc

        protected void LoadDataHangMuc()
        {   
            _hangMuc = Program.Container.Resolve<IService<HangMuc>>();
            hangMucBindingSource.DataSource = _hangMuc.ToList().ToList().Select(p => new { p.Id, p.TenCongTrinh, TenHangSanXuat = p.HangSanXuat.Ten, TenBacChiuLua = p.BacChiuLua.Ten, p.DienTichM2, p.GhiChu, p.SoTang, p.DienTichM3, p.SoPhong, p.TenChatChay, p.DienTichChatChayM2, p.PhieuDieuTraCoSo_Id }).ToList().Where(a => a.PhieuDieuTraCoSo_Id.Equals(_idPhieu)).ToList();
            grvHangMuc.Columns[0].Visible = false;
        }

        private void btnAddHangMuc_Click(object sender, EventArgs e)
        {
            using (var frm = new frmHangMuc())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataHangMuc;
                frm.ShowDialog();
            }
        }

        private void btnEditHangMuc_Click(object sender, EventArgs e)
        {
            if (grvHangMuc.SelectedRows.Count > 0)
            {
                var selectedRow = grvHangMuc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmHangMuc(id))
                {
                    fEdit.OnLoadData += LoadDataHangMuc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnXoaHangMuc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvHangMuc.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvHangMuc.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _hangMuc.Detail(id);
                            _hangMuc.Delete(item);
                            LoadDataHangMuc();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshHangMuc_Click(object sender, EventArgs e)
        {
            LoadDataHangMuc();
        }


        #endregion

        #region PhuongTienPCCC

        protected void LoadDataPhuongTienPcCc()
        {            
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            phuongTienCoSoBindingSource.DataSource = _phuongTienCoSo.ToList().ToList().Select(p => new { p.Id, p.Ten, p.SoLuong, p.TinhTrangHoatDong_Tot, p.TinhTrangHoatDong_TrungBinh, p.TinhTrangHoatDong_Kem, p.TinhTrangHoatDong_HuHong, p.Vitri, p.PhieuDieuTraCoSoId }).ToList().Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu)).ToList();
            grvPhuongTien.Columns[0].Visible = false;
        }

        private void btnAddPhuongTienPCCC_Click(object sender, EventArgs e)
        {
            using (var frm = new frmPhuongTienPCCC())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataPhuongTienPcCc;
                frm.ShowDialog();
            }
        }

        private void btnEditPhuongTien_Click(object sender, EventArgs e)
        {
            if (grvPhuongTien.SelectedRows.Count > 0)
            {
                var selectedRow = grvPhuongTien.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmPhuongTienPCCC(id))
                {
                    fEdit.OnLoadData += LoadDataPhuongTienPcCc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDeletePhuongTien_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvPhuongTien.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvPhuongTien.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _phuongTienCoSo.Detail(id);
                            _phuongTienCoSo.Delete(item);
                            LoadDataPhuongTienPcCc();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshPhuongTien_Click(object sender, EventArgs e)
        {
            LoadDataPhuongTienPcCc();
        }

        #endregion

        #region NguonNuoc

        protected void LoadDataNguonNuoc()
        {
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            nguonNuocBindingSource.DataSource = _nguonNuoc.ToList().ToList().Select(p => new { p.Id, p.Ten, p.KhoiTich, p.MBCC, p.NgoaiCoSo, PhanLoaiChiTietNguonNuoc = p.PhanLoaiChiTietNguonNuoc.Ten, PhanLoaiChiTietNguonNuocId = p.PhanLoaiChiTietNguonNuoc.LoaiNguonNuoc.Ten, p.PhieuDieuTraCoSoesId, p.XeCC, p.TrongCoSo }).ToList().Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu)).ToList();
            dgvNguonNuoc.Columns[0].Visible = false;
        }

        private void btnAddNguonNuoc_Click(object sender, EventArgs e)
        {
            using (var frm = new frmNguonNuoc())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataNguonNuoc;
                frm.ShowDialog();
            }
        }

        private void btnEditNguonNuoc_Click(object sender, EventArgs e)
        {
            if (dgvNguonNuoc.SelectedRows.Count > 0)
            {
                var selectedRow = dgvNguonNuoc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmNguonNuoc(id))
                {
                    fEdit.OnLoadData += LoadDataNguonNuoc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelNguonNuoc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (dgvNguonNuoc.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(dgvNguonNuoc.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _nguonNuoc.Detail(id);
                            _nguonNuoc.Delete(item);
                            LoadDataNguonNuoc();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshNguonNuoc_Click(object sender, EventArgs e)
        {
            LoadDataNguonNuoc();
        }

        #endregion

        #region TuyenTruyenHuanLuyen

        protected void LoadDataTuyenTruyen()
        {
            _tuyenTruyenHuanLuyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            tuyenTruyenHuanLuyenBindingSource.DataSource = _tuyenTruyenHuanLuyen.ToList().ToList()
                .Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu))
                .OrderBy(t => t.Ngay)
                .ToList();
            grvTuyenTruyenHuanLuyen.Columns[0].Visible = false;
        }

        private void btAddTuyenTruyen_Click(object sender, EventArgs e)
        {
            using (var frm = new frmTuyenTruyenHuanLuyen())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataTuyenTruyen;
                frm.ShowDialog();
            }
        }

        private void btEditTuyenTruyen_Click(object sender, EventArgs e)
        {
            if (grvTuyenTruyenHuanLuyen.SelectedRows.Count > 0)
            {
                var selectedRow = grvTuyenTruyenHuanLuyen.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmTuyenTruyenHuanLuyen(id))
                {
                    fEdit.OnLoadData += LoadDataTuyenTruyen;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btDelTuyenTruyen_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvTuyenTruyenHuanLuyen.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvTuyenTruyenHuanLuyen.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _tuyenTruyenHuanLuyen.Detail(id);
                            _tuyenTruyenHuanLuyen.Delete(item);
                            LoadDataTuyenTruyen();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btRefreshTuyenTruyen_Click(object sender, EventArgs e)
        {
            LoadDataTuyenTruyen();

        }

        #endregion

        #region KiemTraAnToan

        protected void LoadDataKiemTraAnToan()
        {
            _kiemTraAnToan = Program.Container.Resolve<IService<KiemTraAnToan>>();
            kiemTraAnToanBindingSource.DataSource = _kiemTraAnToan.ToList().ToList()
                .Select(p => new { p.Id, p.Ngay, CheDoKiemTra = p.CheDoKiemTra.Ten, p.TonTai, p.KienNghi, p.PhieuDieuTraCoSoesId, p.CheDoKiemTraId })
                .OrderBy(p => p.Ngay)
                .Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu)).ToList();
            grvAnToan.Columns[0].Visible = false;
        }

        private void btnAddAnToan_Click(object sender, EventArgs e)
        {
            using (var frm = new frmKiemTraAnToan())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataKiemTraAnToan;
                frm.ShowDialog();
            }
        }

        private void btnEditAnToan_Click(object sender, EventArgs e)
        {
            if (grvAnToan.SelectedRows.Count > 0)
            {
                var selectedRow = grvAnToan.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmKiemTraAnToan(id))
                {
                    fEdit.OnLoadData += LoadDataKiemTraAnToan;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelAnToan_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvAnToan.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvAnToan.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _kiemTraAnToan.Detail(id);
                            _kiemTraAnToan.Delete(item);
                            LoadDataKiemTraAnToan();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshAnToan_Click(object sender, EventArgs e)
        {
            LoadDataKiemTraAnToan();

        }

        #endregion

        #region PhuongAnCC

        private void LoadDataPhuongAnCc()
        {
            _phuongAnChuaChay = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            phuongAnChuaChayBindingSource.DataSource = _phuongAnChuaChay.ToList().ToList()
                .Select(p => new { p.Id, p.Ngay, p.TinhHuong, LucLuongThucTap = p.LucLuongThucTap.Ten, PhuongTienThucTap = p.PhuongTienThucTap.Ten, p.LucLuong_SoLuong, p.PhuongTien_SoLuong, p.KienNghi, p.KetQua, p.PhieuDieuTraCoSoId })
                .Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu))
                .OrderBy(p => p.Ngay)
                .ToList();
            dgvPhuongAnCc.Columns[0].Visible = false;
        }

        private void btnAddPhuongAnCC_Click(object sender, EventArgs e)
        {
            using (var frm = new frmPhuongAnCC())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataPhuongAnCc;
                frm.ShowDialog();
            }
        }

        private void btnEditPhuongAnCC_Click(object sender, EventArgs e)
        {
            if (dgvPhuongAnCc.SelectedRows.Count > 0)
            {
                var selectedRow = dgvPhuongAnCc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmPhuongAnCC(id))
                {
                    fEdit.OnLoadData += LoadDataPhuongAnCc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelPhuongAnCC_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (dgvPhuongAnCc.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(dgvPhuongAnCc.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _phuongAnChuaChay.Detail(id);
                            _phuongAnChuaChay.Delete(item);
                            LoadDataPhuongAnCc();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshPhuongAnCC_Click(object sender, EventArgs e)
        {
            LoadDataPhuongAnCc();
        }

        #endregion

        #region XuLyViPham

        private void LoadDataXuLyViPham()
        {
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            xuLyViPhamBindingSource.DataSource = _xuLyViPham.ToList().ToList()
                .Select(p => new { p.Id, p.Ngay, p.DoiTuongViPham, NhomHanhVi = p.NhomHanhVi.Ten, p.NoiDung, p.CanhCao, p.PhatTien, p.NgayTamDinhChi, p.NgayPhucHoi, p.DinhChi, p.BienPhapXuLyKhac, p.PhieuDieuTraCoSoesId })
                .Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu))
                .OrderBy(x => x.Ngay)
                .ToList();
            grvXuLyViPham.Columns[0].Visible = false;
        }

        private void btnAddXuLyViPham_Click(object sender, EventArgs e)
        {
            using (var frm = new frmXuLyViPham())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataXuLyViPham;
                frm.ShowDialog();
            }
        }

        private void btnEditXuLyViPham_Click(object sender, EventArgs e)
        {
            if (grvXuLyViPham.SelectedRows.Count > 0)
            {
                var selectedRow = grvXuLyViPham.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmXuLyViPham(id))
                {
                    fEdit.OnLoadData += LoadDataXuLyViPham;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelXuLyViPham_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (grvXuLyViPham.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(grvXuLyViPham.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _xuLyViPham.Detail(id);
                            _xuLyViPham.Delete(item);
                            LoadDataXuLyViPham();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btnRefreshXuLyViPham_Click(object sender, EventArgs e)
        {
            LoadDataXuLyViPham();
        }

        #endregion

        #region TinhHinhChayNo

        private void LoadDataTinhHinhChayNo()
        {
            _tinhHinhChayNo = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            tinhHinhChayNoBindingSource.DataSource = _tinhHinhChayNo.ToList().ToList()
                .Select(p => new { p.Id, p.Ngay, p.NguyenNhan, p.NoiPhatSinhChayNo, p.Chet, p.BiThuong, p.TaiSan, p.XuLyHanhChinh, p.KhoiToVuAn, p.PhieuDieuTraCoSoId })
                .Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu))
                .OrderBy (t => t.Ngay)
                .ToList();
            dgvTinhHinhChayNo.Columns[0].Visible = false;
        }

        private void btnAddTinhHinhChayNo_Click(object sender, EventArgs e)
        {
            using (var frm = new frmTinhHinhChayNo())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataTinhHinhChayNo;
                frm.ShowDialog();
            }
        }

        private void btEditTinhhinhchayno_Click(object sender, EventArgs e)
        {
            if (dgvTinhHinhChayNo.SelectedRows.Count > 0)
            {
                var selectedRow = dgvTinhHinhChayNo.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmTinhHinhChayNo(id))
                {
                    fEdit.OnLoadData += LoadDataTinhHinhChayNo;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btDelTinhhinhchayno_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (dgvTinhHinhChayNo.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(dgvTinhHinhChayNo.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _tinhHinhChayNo.Detail(id);
                            _tinhHinhChayNo.Delete(item);
                            LoadDataTinhHinhChayNo();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btF5Tinhhinhchayno_Click(object sender, EventArgs e)
        {
            LoadDataTinhHinhChayNo();
        }

        #endregion

        #region ThayDoiCoSo
        private void LoadDataThayDoiCoSo()
        {
            _thayDoiCoSo = Program.Container.Resolve<IService<ThayDoiCoSo>>();
            thayDoiCoSoBindingSource.DataSource = _thayDoiCoSo.ToList().ToList()
                .Select(p => new { p.Id, p.Ngay, p.NoiDungThayDoi, p.GhiChu, p.PhieuDieuTraCoSoId })
                .Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu))
                .OrderBy(t => t.Ngay)
                .ToList();
            dgvThayDoiCoSo.Columns[0].Visible = false;
        }

        private void btnAddThayDoiCoSo_Click(object sender, EventArgs e)
        {
            using (var frm = new frmThayDoiCoSo())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataThayDoiCoSo;
                frm.ShowDialog();
            }
        }

        private void btEditThayDoiCoSo_Click(object sender, EventArgs e)
        {
            if (dgvThayDoiCoSo.SelectedRows.Count > 0)
            {
                var selectedRow = dgvThayDoiCoSo.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmThayDoiCoSo(id))
                {
                    fEdit.OnLoadData += LoadDataThayDoiCoSo;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btDelThayDoiCoSo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    if (dgvThayDoiCoSo.SelectedRows.Count != 0)
                    {
                        var id = Guid.Parse(dgvThayDoiCoSo.SelectedRows[0].Cells[0].Value.ToString());
                        if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                        {
                            var item = _thayDoiCoSo.Detail(id);
                            _thayDoiCoSo.Delete(item);
                            LoadDataThayDoiCoSo();
                        }
                    }
                    else
                    {
                        AlertList.Error(Constants.Messages.DeleteSelectItemError);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        private void btF5ThayDoiCoSo_Click(object sender, EventArgs e)
        {
            LoadDataThayDoiCoSo();

        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            var item = _phieuDieuTraCoSo.Detail(_idPhieu);
            if (item != null)
            {
                item.NgayLapPhieu = dtNgayLapPhieu.Value;
                item.SoPhieu = txtSoPhieu.Text.Trim();
                item.CanBoQuanLyId = Guid.Parse(drlCanBoQuanLy.SelectedValue.ToString()) ;
                item.CoQuanQuanLyPCCCId = Guid.Parse(drlCoQuanQL.SelectedValue.ToString()) ;
                item.LoaiCoSoId = Guid.Parse(drlLoaiCoSo.SelectedValue.ToString()) ;
                item.TenCoSo = txtTenCoSo.Text.Trim();
                item.Email = txtEmail.Text.Trim();
                item.Website = txtWebsite.Text.Trim();
                item.NamThanhLap = textNamThanhLap.Text;
                item.DienThoai = txtDienThoai.Text;
                item.DiaChi_So = txtDiaChi_So.Text.Trim();
                item.DiaChi_Duong = txtDiaChi_Duong.Text.Trim();
                item.DiaChi_Phuong = txtDiaChi_Phuong.Text.Trim();
                item.QuanHuyenId = Guid.Parse(drlQuanHuyen.SelectedValue.ToString());
                item.DuongDi = txtDuongDi.Text.Trim();
                item.Khu = txtKhu.Text.Trim();
                item.NguoiDungDauCoSo = txtHoTenNguoiDungDauCS.Text.Trim();
                item.DiDong = txtDiDong.Text.Trim();
                item.CoQuanQuanLyTrucTiep = txtCoQuanQLTrucTiep.Text.Trim();
                item.CapQuanLyHanhChinhId = Guid.Parse(drlCapQuanLyHanhChinh.SelectedValue.ToString());
                item.NuocDauTu = txtNuocDauTu.Text.Trim();
                item.HinhThucDauTuId = Guid.Parse(drlHinhThucDauTu.SelectedValue.ToString());
                item.ThanhPhanKinhTeId = Guid.Parse(drlThanhPhanKinhTe.SelectedValue.ToString());
                item.LoaiHinhHoatDongId = Guid.Parse(drlLoaiHinhHoatDong.SelectedValue.ToString());
                item.NganhId = Guid.Parse(drlNganh.SelectedValue.ToString());
                item.TinhChatHoatDong = txtTinhChatHoatDong.Text.Trim();
                if (radDamua.Checked)
                {
                    item.CoSoThuocDienMuaBHCNBB = true;
                }
                else
                {
                    item.CoSoThuocDienMuaBHCNBB = false;
                }

                if (radKhongMua.Checked)
                {
                    item.CoSoKhongThuocDienMuaBHCNBB = true;
                }
                else
                {
                    item.CoSoKhongThuocDienMuaBHCNBB = false;
                }

                item.LLPCCC_TongSoNguoi = (int?)txtLucLuong_TongSo.Value;
                item.LLPCCC_SoNguoiDuocCapGiayPhep = (int?)txtLucLuong_SoNguoiDuocCapGiay.Value;
                item.LLPCCC_TrongGioLamViec = (int?)txtLucLuong_TrongGioLamViec.Value;
                item.LLPCCC_NgoaiGioLamViec = (int?)txtLucLuong_NgoaiGioLamViec.Value;
                item.LLPCCC_BoPhanCongTac = txtLucLuong_BoPhanCongTac.Text.Trim();
                item.LLPCCC_HoTenDoiTruong = txtLucLuong_TenDoiTruong.Text.Trim();
                item.LLPCCC_DienThoaiCoDinh = txtLucLuong_DienThoai.Text.Trim();
                item.LLPCCC_DienThoaiDiDong = txtLucLuong_DiDong.Text.Trim();
                item.KT_SoLanKiemTra = (int?)txtKiemTra_SoLan.Value;
                item.TT_NgayXayDungPA = dtPACC_Ngay.Value;
                //item.TT_PA = txtPACC_TT.Text.Trim();
                item.CapPheDuyetId = Guid.Parse(drlCapPheDuyet.SelectedValue.ToString());
                item.Status = true;
                _phieuDieuTraCoSo.Update(item);

                var frm = new frmPhieuDieuTraCoSoList();
                frm.MdiParent = MdiParent;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }

    }
}
